import React from 'react'
import CustomMenu from "../CustomMenu/index";
import {logout} from "../../utils/Session";

class SiderNav extends React.Component {
  state = {
    menus: []
  }

  componentDidMount() {
    this.createMenuTree()
  }

  createMenuTree() {
    let menuTreeStr = localStorage.getItem("menuTree");
    console.log("menuTree",menuTreeStr)
    if (menuTreeStr == null){
      alert("用户已过期，请重新登录")
      logout();
      return;
    }
    if (typeof menuTreeStr == undefined){
      alert("权限不足，请重新登录")
      logout();
      return;
    }

    let menuTree = JSON.parse(menuTreeStr);
    if (menuTree.length <= 0){
      alert("权限不足，请重新登录")
      logout();
      return;
    }
   let dataList = this.createTree(menuTree);
    this.setState({
      menus: dataList
    })
  }

  createTree(menuTree){
    let dataList = [];
    for (let i = 0; i < menuTree.length; i++) {
      let comData = menuTree[i];
      const data = {};
      data.title = comData.menuName;//name
      data.icon = comData.icon;//图标
      data.key = comData.menuCode;//url
      if(comData.children.length > 0){
        data.subs = this.createTree(comData.children);
      }
      dataList[i] = data;
    }
    return dataList;
  }

  render() {
    return (
      <div style={{height: '100vh',overflowY:'scroll'}}>
        <div style={styles.logo}>智能公寓管理平台</div>
        <CustomMenu menus={this.state.menus}/>
      </div>
    )
  }

}

const styles = {
  logo: {
    height: '50px',
    lineHeight: '50px',
    color: '#fff',
    fontSize: '18px',
    textAlign: 'center',
    backgroundColor: '#002140'
  }
}

export default SiderNav
